package com.afiorenza.one;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import com.afiorenza.one.enums.TipoTransporte;
import com.afiorenza.one.factory.VehiculoInterface;
import com.afiorenza.one.impl.Colectivo;
import com.afiorenza.one.impl.Taxi;

public class VehiculoFactory {
	final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

	public VehiculoInterface getVehiculo(List<Object> l) {
		Iterator<Object> i = l.iterator();

		if (i.hasNext()) {
			String s = (String) i.next();

			if (s.equalsIgnoreCase("COLECTIVO")) {
				return this.getColectivo(i);
			} else if (s.equalsIgnoreCase("TAXI")) {
				return this.getTaxi(i);
			}
		}
		return null;
	}

	private void setDefaut(VehiculoInterface vehiculo, Iterator<Object> i) {
		try {
			vehiculo.setCantidadRuedas(Integer.valueOf(i.next().toString()));
			vehiculo.setFechaPatentamiento(dateFormat.parse(i.next().toString()));
			vehiculo.setNumeroPasajeros(Integer.valueOf(i.next().toString()));
			vehiculo.setTipoTransporte(TipoTransporte.getTipoTransporte(i.next().toString()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	private Colectivo getColectivo(Iterator<Object> i) {
		Colectivo colectivo = new Colectivo();

		this.setDefaut(colectivo, i);
		colectivo.setLinea(i.next().toString());
		colectivo.setNumeroInterno(Integer.valueOf(i.next().toString()));

		return colectivo;
	}

	private Taxi getTaxi(Iterator<Object> i) {
		Taxi taxi = new Taxi();

		this.setDefaut(taxi, i);
		taxi.setNombreChofer(i.next().toString());
		taxi.setNumeroInterno(i.next().toString());
		taxi.setEmpresaTaxi(i.next().toString());

		return taxi;

	}

}
